package com.abeir.bilibili.convert;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.Optional;

public class TitleParser implements  Parser<Optional<TitleInfo>> {

    @Override
    public Optional<TitleInfo> parse(File file) throws IOException {
        byte[] data = Files.readAllBytes(file.toPath());
        JSONObject json = JSON.parseObject(data);
        TitleInfo info = new TitleInfo();
        info.title = json.getString("title");
        info.groupTitle = json.getString("groupTitle");
        info.groupPart = json.getInteger("p");
        if(info.title == null || info.title.isEmpty()) {
            return Optional.empty();
        }
        return Optional.of(info);
    }
}
